package com.xdjmall.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class DBUtils{

	private static DataSource dataSource;
	
	static {
		try {
			Properties pro = new Properties();
			InputStream is = new FileInputStream("D:\\workspace\\2014\\javawebworkspace\\xdjmall\\config\\jdbc.properties");
			pro.load(is);
			String driver = pro.getProperty("jdbc.driver");
			String url = pro.getProperty("jdbc.url");
			String name = pro.getProperty("jdbc.username");
			String pwd = pro.getProperty("jdbc.password");
			String size = pro.getProperty("jdbc.initialSize");
			String maxActive = pro.getProperty("jdbc.maxActive");
			//创建Druid连接池
			DruidDataSource dds = new DruidDataSource();
			dds.setDriverClassName(driver);
			dds.setUrl(url);
			dds.setUsername(name);
			dds.setPassword(pwd);
			dds.setInitialSize(Integer.valueOf(size));
			dds.setMaxActive(Integer.valueOf(maxActive));
			//连接池的实现赋值给接口
			dataSource = dds;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
}
